package cn.com.service.impl.sys;

import static cn.com.support.message.MessageCodeEnum.*;

import java.util.List;

import javax.management.relation.Role;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.com.commons.Paginator;
import cn.com.commons.utils.NumberUtil;
import cn.com.dal.dao.hr.RoleDAO;
import cn.com.dal.dataobject.hr.RoleDO;
import cn.com.service.sys.RoleService;
import cn.com.support.RollBackException;
import cn.com.support.dal.DAOException;
import cn.com.support.message.MessageResult;

@Service
public class RoleServiceImpl implements RoleService {

	private final Logger logger = LoggerFactory
			.getLogger(RoleServiceImpl.class);

	@Autowired
	private RoleDAO<RoleDO> roleDAO;

	@Override
	public MessageResult<Long> save(RoleDO obj) {
		MessageResult<Long> result = new MessageResult<Long>(false, null,
				ERROR_INSERT);
		Long id = null;
		try {
			// 验证是否重复
			RoleDO role = new RoleDO();
			role.setName(obj.getName());
			List<RoleDO> roleList = roleDAO.selectList(role);
			
			//查看数据中是否存在
			RoleDO erole=roleDAO.selectById(obj.getId());
			
			if (erole!=null && (!NumberUtil.isPositive(roleList.size()) || erole.getId().equals(roleList.get(0).getId())) ){
				//在数据库中存在，并且名称不重复，如果重复,则必须是要更新的数据,为要跟新的数据
				roleDAO.updateById(obj);
				result.setMessageResult(true, id, INFO_SUCCEED);
				return result;
			}else if (erole==null && !NumberUtil.isPositive(roleList.size())) {
				//如果在数据库中不存在，并且没有重复，则为新增
				id = roleDAO.insert(obj);
				if (NumberUtil.isPositive(id)) {
					result.setMessageResult(true, id, INFO_SUCCEED);
					return result;
				}
			}else{
				//否则名称重复
				result.setMessageResult(false, obj.getId(), ERROR_REPEAT);
				result.builidMessage("角色名【"+obj.getName()+"】已经存在");
				return result;
			}
		} catch (DAOException e) {
			logger.error(String.format(ERROR_INSERT.descript() + ":[%d]", id),
					e);
			result.setMessageResult(false, id, ERROR_INSERT);
		}
		return result;
	}

	@Override
	public MessageResult<Integer> updateById(RoleDO obj) {
		MessageResult<Integer> result = new MessageResult<Integer>(false, null,
				ERROR_UPDATE);
		Integer count = null;
		try {
			count = roleDAO.updateById(obj);
			if (NumberUtil.isPositive(count)) {
				result.setMessageResult(true, count, INFO_SUCCEED);
			}
		} catch (DAOException e) {
			logger.error(String.format(ERROR_UPDATE.descript() + ":[%s]", obj),
					e);
			result.setMessageResult(false, count, ERROR_UPDATE);
		}
		return result;
	}

	@Override
	@Transactional
	public MessageResult<Integer> delete(Long[] id) {
		MessageResult<Integer> result = new MessageResult<Integer>(false, null,
				ERROR_SELECT);
		for (int i = 0; i < id.length; i++) {
			Integer count = null;
			try {
				count = roleDAO.deleteById(id[i]);
				if (NumberUtil.isPositive(count)) {
					result.setMessageResult(true, count, INFO_SUCCEED);
				} else {
					TransactionAspectSupport.currentTransactionStatus()
							.setRollbackOnly();
					result.setMessageResult(false, 0, ERROR_SELECT);
					return result;
				}
			} catch (DAOException e) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				logger.error(
						String.format(ERROR_DELETE.descript() + ":[%d]", id[i]),
						e);
				result.setMessageResult(false, count, ERROR_SELECT);
			}
		}
		return result;
	}

	@Override
	public <C> List<RoleDO> selectList(C condition) {
		try {
			return roleDAO.selectList(condition);
		} catch (DAOException e) {
			logger.error(
					String.format(ERROR_SELECT.descript() + ":[%s]", condition),
					e);
		}
		return null;
	}

	@Override
	public MessageResult<RoleDO> selectById(Long id) {
		MessageResult<RoleDO> result = new MessageResult<RoleDO>(false, null,
				ERROR_SELECT);
		RoleDO roleDO = null;
		try {
			roleDO = roleDAO.selectById(id);
			result.setMessageResult(true, roleDO, INFO_SUCCEED);
		} catch (DAOException e) {
			logger.error(String.format(ERROR_SELECT.descript() + ":[%d]", id),
					e);
			result.setMessageResult(false, roleDO, ERROR_SELECT);
		}
		return result;
	}

	@Override
	public <C> Paginator<C, List<RoleDO>> selectForPage(
			Paginator<C, List<RoleDO>> paginator) {
		Paginator<C, List<RoleDO>> result = null;
		try {
			result = roleDAO.selectForPage(paginator);
		} catch (DAOException e) {
			logger.error(
					String.format(ERROR_SELECT.descript() + ":[%s]",
							paginator.getCondition()), e);
		}
		return result;
	}

}
